<?php
/**
*
* @package ppkBB3cker
* @version $Id: install_update.php 1.000 2010-01-26 15:25:00 PPK $
* @copyright (c) 2010 PPK
* http://www.ppkbb3cker.ru, http://protoneutron.narod.ru
*
*/

error_reporting(0);

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require($phpbb_root_path.'config.'.$phpEx);

if($dbms!='mysql')
{
	err('Only mysql supported');
}

$c=@mysql_connect($dbhost, $dbuser, $dbpasswd);
if(!$c)
{
	err('Error connecting database: '.mysql_error());
}

$s=@mysql_select_db($dbname, $c);
if(!$s)
{
	err('Error selecting database: '.mysql_error());
}

$result=my_sql_query('SELECT VERSION() AS version');
$mv=mysql_fetch_row($result);

$m_version=$mv[0];

if(version_compare($m_version, '4.1.3', '>='))
{
	$mysql_dbms=41;
}
else
{
	$mysql_dbms=40;
}

my_sql_query("SET NAMES 'utf8'");

if($mysql_dbms==41)
{
	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_af` (
	`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
	`post_msg_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
	`topic_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
	`field_name` varchar(32) COLLATE utf8_bin NOT NULL,
	`field_value` text CHARACTER SET utf8 NOT NULL,
	`i_width` smallint(4) unsigned NOT NULL DEFAULT '0',
	`i_height` smallint(4) unsigned NOT NULL DEFAULT '0',
	`f_size` int(20) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`),
	KEY `post_msg_id` (`post_msg_id`),
	KEY `topic_id` (`topic_id`),
	KEY `field_name` (`field_name`),
	FULLTEXT KEY `field_value` (`field_value`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_afsets` (
	`id` mediumint(8) NOT NULL AUTO_INCREMENT,
	`af_id` smallint(5) unsigned NOT NULL DEFAULT '0',
	`af_name` varchar(32) COLLATE utf8_bin NOT NULL DEFAULT '',
	`af_count` tinyint(1) unsigned NOT NULL DEFAULT '0',
	`af_required` tinyint(1) unsigned NOT NULL DEFAULT '0',
	`af_order` smallint(3) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`),
	KEY `af_id` (`af_id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_aftpl` (
	`id` smallint(5) NOT NULL AUTO_INCREMENT,
	`af_name` varchar(64) COLLATE utf8_bin NOT NULL,
	`af_descr` text COLLATE utf8_bin NOT NULL,
	`af_subject` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
	`af_data` text COLLATE utf8_bin NOT NULL,
	PRIMARY KEY (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

	$result=my_sql_query("ALTER TABLE `{$table_prefix}forums` ADD `forum_addfields2` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT ''");
	$result=my_sql_query("ALTER TABLE `{$table_prefix}forums` ADD `tprivate_flag` CHAR( 1 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT ''");
}
else
{
	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_af` (
	`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
	`post_msg_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
	`topic_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
	`field_name` varchar(32) binary NOT NULL,
	`field_value` text NOT NULL,
	`i_width` smallint(4) unsigned NOT NULL DEFAULT '0',
	`i_height` smallint(4) unsigned NOT NULL DEFAULT '0',
	`f_size` int(20) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`),
	KEY `post_msg_id` (`post_msg_id`),
	KEY `topic_id` (`topic_id`),
	KEY `field_name` (`field_name`),
	FULLTEXT KEY `field_value` (`field_value`)
	) TYPE=MyISAM");

	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_afsets` (
	`id` mediumint(8) NOT NULL AUTO_INCREMENT,
	`af_id` smallint(5) unsigned NOT NULL DEFAULT '0',
	`af_name` varchar(32) binary NOT NULL DEFAULT '',
	`af_count` tinyint(1) unsigned NOT NULL DEFAULT '0',
	`af_required` tinyint(1) unsigned NOT NULL DEFAULT '0',
	`af_order` smallint(3) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`),
	KEY `af_id` (`af_id`)
	) TYPE=MyISAM");

	$result=my_sql_query("CREATE TABLE `{$table_prefix}tracker_aftpl` (
	`id` smallint(5) NOT NULL AUTO_INCREMENT,
	`af_name` varchar(64) binary NOT NULL,
	`af_descr` text NOT NULL,
	`af_subject` varchar(255) binary NOT NULL DEFAULT '',
	`af_data` text NOT NULL,
	PRIMARY KEY (`id`)
	) TYPE=MyISAM");

	$result=my_sql_query("ALTER TABLE `{$table_prefix}forums` ADD `forum_addfields2` varchar(255) binary NOT NULL DEFAULT ''");

	$result=my_sql_query("ALTER TABLE `{$table_prefix}forums` ADD `tprivate_flag` char(1) binary NOT NULL DEFAULT ''");
}

$result=my_sql_query("UPDATE `{$table_prefix}forums` SET forum_addfields2=forum_addfields");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` ADD `seeder_new` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("SELECT id FROM `{$table_prefix}tracker_peers` WHERE seeder='yes'");

while($row=mysql_fetch_row($result))
{
	$result2=my_sql_query("UPDATE `{$table_prefix}tracker_peers` SET seeder_new='1' WHERE id='{$row[0]}'", true);
}

echo 'Update PEERS SEEDER<br /><br />';

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` DROP `seeder`");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` CHANGE `seeder_new` `seeder` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");



$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` ADD `connectable_new` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("SELECT id FROM `{$table_prefix}tracker_peers` WHERE connectable='yes'");

while($row=mysql_fetch_row($result))
{
	$result2=my_sql_query("UPDATE `{$table_prefix}tracker_peers` SET connectable_new='1' WHERE id='{$row[0]}'", true);
}

echo 'Update PEERS CONNECTABLE<br /><br />';

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` DROP `connectable`");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` CHANGE `connectable_new` `connectable` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");


$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` DROP KEY `ip`");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` DROP KEY `info_hash`");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` ADD KEY `ip` (`ip`)");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD UNIQUE KEY `info_hash` (`info_hash`)");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD INDEX (`post_msg_id`)");
$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE config_name='ppkbb_gtcignore_connectable'");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_snatched` DROP `seeder`");
$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_peers` ADD INDEX (`seeder`)");

$result=my_sql_query("ALTER TABLE `{$table_prefix}users` ADD `user_shadow_uploaded` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}topics` ADD `topic_addfields_poster` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");
$result=my_sql_query("ALTER TABLE `{$table_prefix}topics` ADD `topic_first_post_show` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL");

$result=my_sql_query("ALTER TABLE `{$table_prefix}forums` ADD `forum_first_post_show` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'u_canusefpep', '1', '0', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'f_canusefpep', '0', '1', '0')");

$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'u_canuseup', '1', '0', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'f_canuseup', '0', '1', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'u_cansetup', '1', '0', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}acl_options` (`auth_option_id`, `auth_option`, `is_global`, `is_local`, `founder_only`) VALUES (NULL, 'f_cansetup', '0', '1', '0')");

$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_fpep_enable', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_addfields_type', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_trclear_cronjobs', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_addfields_pos', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_postscr_fields', 'screenshots poster')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_postscr_src', '1')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_tstatus_notify', '0 0')");

$result=my_sql_query("UPDATE `{$table_prefix}users` SET user_shadow_uploaded=user_uploaded");

mysql_close();
exit();

function my_sql_query($query, $silent=0)
{
	$result=@mysql_query($query);
	if(!$silent)
	{
		echo ($result ? '<b>Ok</b>: ' : '<span style="color:#FF0000"><b>Error</b>: ').$query.($result ? '' : '</span><br />['.mysql_errno().'] ('.mysql_error().')').'<br /><br />';
	}

	return $result;
}

function err($str)
{
	echo $str;
	mysql_close();
	exit();
}

?>
